<!--
 * @Description: 点菜页面部位面板组件（右侧面板）
-->
<template>
  <div class="practice-panel" v-if="currentFoodData">
    <div class="title">{{ currentFoodData.name }}部位</div>

    <div class="selected-box">
      <div class="label">已选：</div>
      <div class="chips">
        <div class="chip" v-for="(name, idx) in selectedNames" :key="name + idx">
          <div class="ellipsis">{{ name }}</div>
          <el-icon class="close-icon" @click.stop="deleteChip(name)"><Close /></el-icon>
        </div>
      </div>
    </div>

    <div class="category-box-content">
      <div>
        <div class="list-box" v-if="presetList.length">
          <div
            class="list-item"
            :class="{ active: isSelected(item) }"
            v-for="item in presetList"
            :key="item"
            @click="toggleSelect(item)"
          >
            <div class="name">{{ item }}</div>
          </div>
          <div
            class="list-item-zhanwei"
            v-for="val in ['a', 'b', 'c', 'd']"
            :key="'zw' + val"
          ></div>
        </div>
        <div class="no-data-box" v-else>
          <NotData desc="暂无常用部位" />
        </div>
      </div>

      <div class="count-choose-box">
        <div class="selected"></div>
        <div class="total" v-if="selectedNames.length">共{{ selectedNames.length }}项</div>
      </div>
    </div>

    <div class="btn-box">
      <div class="desc">
        <span v-if="selectedNames.length">已选：（{{ selectedNames.join('、') }} ）</span>
      </div>
      <div class="two-btn">
        <el-button class="delete-btn" @click="handleBack">返回</el-button>
        <el-button class="delete-btn" @click="handleClearAll">全部清除</el-button>
        <el-button class="confirm-btn" :loading="loading" @click="handleConfirm"
          >确认修改</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { ElIcon } from 'element-plus'
import { Close } from '@element-plus/icons-vue'
import NotData from '@/components/NotData/Index.vue'

interface Props {
  currentFoodData?: any
}
interface Emits {
  (e: 'back'): void
  (e: 'confirm', payload: { position: string[]; selectedNames?: string[] }): void
}

const props = withDefaults(defineProps<Props>(), { currentFoodData: null })
const emit = defineEmits<Emits>()

const loading = ref(false)
const presetList = ref<string[]>(['头', '腰', '尾', '身'])
const selectedList = ref<string[]>([])

watch(
  () => props.currentFoodData,
  newVal => {
    if (newVal) {
      selectedList.value = Array.isArray((newVal as any).position)
        ? (newVal as any).position.slice()
        : []
    }
  },
  { immediate: true }
)

const selectedNames = computed(() => selectedList.value)
const isSelected = (name: string) => selectedList.value.includes(name)
const toggleSelect = (name: string) => {
  const idx = selectedList.value.indexOf(name)
  if (idx > -1) selectedList.value.splice(idx, 1)
  else selectedList.value.push(name)
}

const deleteChip = (name: string) => {
  const idx = selectedList.value.indexOf(name)
  if (idx > -1) selectedList.value.splice(idx, 1)
}

const handleClearAll = () => {
  selectedList.value = []
}
const handleConfirm = () => {
  emit('confirm', { position: selectedList.value, selectedNames: selectedList.value })
}
const handleBack = () => emit('back')
</script>

<style scoped lang="scss">
.practice-panel {
  box-sizing: border-box;
  width: 976px;
  height: 986px;
  background: #ffffff;
  border-radius: 20px;
  box-shadow: 3px 0px 24px 0px rgba(0, 0, 0, 0.16);

  .title {
    height: 88px;
    background: #f4f4f4;
    border-radius: 20px 20px 0 0;
    line-height: 88px;
    font-size: 18px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #353535;
  }

  .selected-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 32px;
    height: 92px;
    box-sizing: border-box;

    .label {
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .chips {
      display: flex;
      flex: 1;
      width: 0;
      overflow: auto;
      gap: 20px;
      .chip {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100px;
        flex-shrink: 0;
        padding: 0 10px;
        height: 52px;
        background: #7c0a03;
        border-radius: 12px;
        font-size: 18px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: center;
        color: #fff;
        line-height: 52px;
      }
    }
  }

  .list-box {
    padding: 0px 20px 20px;
    height: 580px;
    box-sizing: border-box;
    overflow-y: auto;
    background: #f4f4f4;
    display: grid;
    grid-template-columns: repeat(4, 1fr);

    .list-item {
      min-width: 138px;
      padding: 0 10px;
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      height: 70px;
      background: #ffffff;
      border-radius: 12px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      margin-right: 10px;
      color: #333333;
      cursor: pointer;
      margin-top: 20px;

      .name {
        font-size: 18px;
        margin-bottom: 4px;
        text-align: center;
      }
    }
    .active {
      border: 1px solid #7c0a03;
      color: #7c0a03;
    }
  }

  .btn-box {
    padding: 0 30px;
    height: 112px;
    border-radius: 0 0 20px 20px;
    box-sizing: border-box;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .desc {
      font-size: 16px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      color: #353535;
      line-height: 24px;
      max-width: 300px;
    }
    .two-btn {
      display: flex;
      align-items: center;
    }
    .delete-btn {
      width: 100px;
      height: 52px;
      line-height: 52px;
      background: #ffffff;
      border: 1px solid #333333;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #333333;
      cursor: pointer;
      margin-right: 20px;
    }
    .confirm-btn {
      width: 200px;
      height: 52px;
      background: #7c0a03;
      border-radius: 12px;
      line-height: 52px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #ffffff;
      cursor: pointer;
      margin-left: 20px;
    }
  }
}
.category-box-content {
  padding: 20px;
  background-color: #f4f4f4;
  height: 640px;
}
.count-choose-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
}
.close-icon {
  cursor: pointer;
}
</style>
